﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data.OracleClient;
using DreamBuilder.Common;


namespace Web.SystemConfig.DataAccess
{
    public class DALDocument_CategoryManage : DataAccessBase
    {
        internal DataTable CheckDeptParentId(string parentId)
        {
            DbParameter[] arrParam;
            string strSql = string.Empty;
            if (DBHelper.database is Microsoft.Practices.EnterpriseLibrary.Data.Oracle.OracleDatabase)
            {
                arrParam = new DbParameter[] {
                    new OracleParameter(":Parent_ID", parentId) 
                };
                strSql = "SELECT * FROM P_Auth_ORG where Parent_ID=:Parent_ID ";
            }
            else
            {
                arrParam = new DbParameter[] {
                    new SqlParameter("@Parent_ID", parentId)
                };
                strSql = "SELECT * FROM P_Auth_ORG where Parent_ID=@Parent_ID ";
            }
            return DBHelper.ExecuteDataSet(strSql, arrParam).Tables[0];
        }
        internal DataTable GetORGInfo(string ORG_ID)
        {
            string strSql = " SELECT * FROM P_Auth_ORG where ORG_ID = '" + ORG_ID + "'";
            return DBHelper.ExecuteDataSet(strSql).Tables[0];
        }

        internal DataTable GetAllOrgData()
        {
            string strSql = "SELECT * FROM P_Auth_ORG where [Status]!='Org_Status--0'";
            return DBHelper.ExecuteDataSet(strSql).Tables[0];
        }
        internal int IsParentID()
        {
            string strsql = "SELECT COUNT(*) FROM P_Auth_ORG";
            int obj = (int)int.Parse(DBHelper.ExecuteScalar(strsql).ToString());
            return obj;
        }
        internal void DeleteOldRoleData(string ORG_ID)
        {
            string strSql = " DELETE FROM P_Auth_ORG_Role where ORG_ID = '" + ORG_ID + "'";
            DBHelper.ExecuteSql(strSql);
        }

        internal void SaveORG_Role(List<string> listRole_ID, string ORG_ID)
        {
            StringBuilder sb = new StringBuilder();
            foreach (string roleID in listRole_ID)
            {
                string sql = " INSERT INTO P_Auth_ORG_Role VALUES('" + roleID + "','" + ORG_ID + "'); \n ";
                sb.Append(sql);
            }
            if (!string.IsNullOrEmpty(sb.ToString()))
            {
                DBHelper.ExecuteSql(sb.ToString());
            }
        }
        #region add by 刘英男 检查单位编号是否唯一
        /// <summary>
        ///  检查单位编号是否唯一
        /// </summary>
        /// <param name="checkOrgCode">登录名</param>
        /// <returns>数量</returns>
        internal int CheckORG_Code(string checkOrgCode)
        {
            string sql = "select Count(*) from p_auth_org where org_code='" + checkOrgCode + "'";
            int obj = (int)int.Parse(DBHelper.ExecuteScalar(sql).ToString());
            return obj;
        }
        #endregion

        #region add by 刘英男 检查能否是历史单位 20111108
        /// <summary>
        ///  检查能否是历史单位
        /// </summary>
        /// <param name="ORG_ID">所属单位编号</param>
        /// <returns>数量</returns>
        public int checkHistoryORGInfo(string ORG_ID)
        {
            string sql = "select Count(*) from p_auth_org where parent_id='" + ORG_ID + "'";
            int obj = (int)int.Parse(DBHelper.ExecuteScalar(sql).ToString());
            return obj;
        }
        #endregion
        #region add by 刘英男 检查能否是历史单位的用户20111108
        /// <summary>
        ///  检查能否是历史单位的用户
        /// </summary>
        /// <param name="ORG_ID">所属单位编号</param>
        /// <returns>数量</returns>
        public int checkHistoryORGUserInfo(string ORG_ID)
        {
            string sql = "select Count(*) from p_auth_user where org_id='" + ORG_ID + "'";
            int obj = (int)int.Parse(DBHelper.ExecuteScalar(sql).ToString());
            return obj;
        }
        #endregion
    }
}
